/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/* ---------------------------- */
/* XMLHTTPRequest Enable */
/* ---------------------------- */
function createObject() {
var request_type;
var G_OrderID;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_type = new XMLHttpRequest();
}
return request_type;
}

var http = createObject();

/* -------------------------- */
/* INSERT */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */
var nocache = 0;

function Request(page, action, id){

    if(page == 'user'){
        
//        var command = encodeURI(document.getElementById('action').value)
        
        if('create' == action){
            var ID = encodeURI(document.getElementById('ID').value);
            var Email = encodeURI(document.getElementById('Email').value);
            var LastName = encodeURI(document.getElementById('LastName').value);
            var Name = encodeURI(document.getElementById('Name').value);
            var MiddleName = encodeURI(document.getElementById('MiddleName').value);

            var request = '/core/User/UserAction.php' + '?' + 'action=create'
                           + '&id=' + ID
                           + '&email=' + Email
                           + '&name=' + Name 
                           + '&lastname=' + LastName
                           + '&middlename=' + MiddleName
                       ; 

            nocache = Math.random();
    //         Pass the login variables like URL variable
            http.open('post', request+'&nocache = '+nocache);
            http.onreadystatechange = UserReply;
            http.send(null);
        }else if('update' == action){
            ID = encodeURI(document.getElementById('id').value);
            Number = encodeURI(document.getElementById('number').value);
            Email = encodeURI(document.getElementById('Email').value);
            LastName = encodeURI(document.getElementById('LastName').value);
            Name = encodeURI(document.getElementById('Name').value);
            MiddleName = encodeURI(document.getElementById('MiddleName').value);

            var request = '/core/User/UserAction.php' + '?' + 'action=update'
                           + '&id=' + ID
                           + '&number=' + Number
                           + '&email=' + Email
                           + '&name=' + Name 
                           + '&lastname=' + LastName
                           + '&middlename=' + MiddleName
                       ; 

            nocache = Math.random();
    //         Pass the login variables like URL variable
            http.open('post', request+'&nocache = '+nocache);
            http.onreadystatechange = UserReply;
            http.send(null);
        }else if('delete' == action){
//            alert('delete' + id);
            if(confirm('Удалить учетную запись?')){
//                alert('yes, delete!')
               var request = '/core/User/UserAction.php' + '?' + 'action=delete' + '&id=' + id
                       ; 

                nocache = Math.random();
        //         Pass the login variables like URL variable
                http.open('post', request+'&nocache = '+nocache);
                http.onreadystatechange = UserReply;
                http.send(null);
            }
        }
	}
	//------------------- MatGroup --------------------------------------------------------
		else if(page == 'MatGroup'){
        
						if('create' == action){
							var Name = encodeURI(document.getElementById('Name').value);

							var request = '/core/MatGroup/MatGroupAction.php' + '?' + 'action=create'
										   + '&name=' + Name 
									   ; 

							nocache = Math.random();
					//         Pass the login variables like URL variable
							http.open('post', request+'&nocache = '+nocache);
							http.onreadystatechange = MatGroupReply;
							http.send(null);
						}else if('update' == action){
							ID = encodeURI(document.getElementById('id').value);
							Name = encodeURI(document.getElementById('Name').value);

							var request = '/core/MatGroup/MatGroupAction.php' + '?' + 'action=update'
										   + '&id=' + ID
										   + '&name=' + Name 
									   ; 

							nocache = Math.random();
					//         Pass the login variables like URL variable
							http.open('post', request+'&nocache = '+nocache);
							http.onreadystatechange = MatGroupReply;
							http.send(null);
						}else if('delete' == action){
							if(confirm('Удалить?')){
							   var request = '/core/MatGroup/MatGroupAction.php' + '?' + 'action=delete' + '&id=' + id
									   ; 
								nocache = Math.random();
						//         Pass the login variables like URL variable
								http.open('post', request+'&nocache = '+nocache);
								http.onreadystatechange = MatGroupReply;
								http.send(null);
							}
							   
						}else 
							alert('Unknown command');
		//MatGroup
    }
	//------------------- Dimension --------------------------------------------------------
		else if(page == 'Dimension'){
        
						if('create' == action){
							var Name = encodeURI(document.getElementById('Name').value);

							var request = '/core/Dimension/DimensionAction.php' + '?' + 'action=create'
										   + '&name=' + Name 
									   ; 

							nocache = Math.random();
					//         Pass the login variables like URL variable
							http.open('post', request+'&nocache = '+nocache);
							http.onreadystatechange = DimensionReply;
							http.send(null);
						}else if('update' == action){
							ID = encodeURI(document.getElementById('id').value);
							Name = encodeURI(document.getElementById('Name').value);

							var request = '/core/Dimension/DimensionAction.php' + '?' + 'action=update'
										   + '&id=' + ID
										   + '&name=' + Name 
									   ; 

							nocache = Math.random();
					//         Pass the login variables like URL variable
							http.open('post', request+'&nocache = '+nocache);
							http.onreadystatechange = DimensionReply;
							http.send(null);
						}else if('delete' == action){
							if(confirm('Удалить?')){
							   var request = '/core/Dimension/DimensionAction.php' + '?' + 'action=delete' + '&id=' + id
									   ; 
								nocache = Math.random();
						//         Pass the login variables like URL variable
								http.open('post', request+'&nocache = '+nocache);
								http.onreadystatechange = DimensionReply;
								http.send(null);
							}
							   
						}else 
							alert('Unknown command');
		
	}
	//------------------- Material --------------------------------------------------------
		else if(page == 'Material'){
        
						if('create' == action){
							var Code = encodeURI(document.getElementById('Code').value);
							var Name = encodeURI(document.getElementById('Name').value);
							var MatGroup = encodeURI(document.getElementById('MatGroup').value);
							var MatDimension = encodeURI(document.getElementById('MatDimension').value);
							var Price1 = encodeURI(document.getElementById('Price1').value);
							var Price2 = encodeURI(document.getElementById('Price2').value);
							var Active = encodeURI(document.getElementById('Active').value);
							var Ball = encodeURI(document.getElementById('Ball').value);

							var request = '/core/Material/MaterialAction.php' + '?' + 'action=create'
								+ '&code=' + Code
								+ '&name=' + Name
								+ '&matgroup=' + MatGroup
								+ '&matdimension=' + MatDimension
								+ '&price1=' + Price1
								+ '&price2=' + Price2
								+ '&ball=' + Ball
								+ '&active=' + Active
								;
								

							nocache = Math.random();
					//         Pass the login variables like URL variable
							http.open('post', request+'&nocache = '+nocache);
							http.onreadystatechange = MaterialReply;
							http.send(null);
						}else if('update' == action){
							var ID = encodeURI(document.getElementById('ID').value);
							var Code = encodeURI(document.getElementById('Code').value);
							var Name = encodeURI(document.getElementById('Name').value);
							var MatGroup = encodeURI(document.getElementById('MatGroup').value);
							var MatDimension = encodeURI(document.getElementById('MatDimension').value);
							var Price1 = encodeURI(document.getElementById('Price1').value);
							var Price2 = encodeURI(document.getElementById('Price2').value);
							var Active = encodeURI(document.getElementById('Active').value);
							var Ball = encodeURI(document.getElementById('Ball').value);

							var request = '/core/Material/MaterialAction.php' + '?' + 'action=update'
								+ '&id=' + ID
								+ '&code=' + Code
								+ '&name=' + Name
								+ '&matgroup=' + MatGroup
								+ '&matdimension=' + MatDimension
								+ '&price1=' + Price1
								+ '&price2=' + Price2
								+ '&ball=' + Ball
								+ '&active=' + Active
								;
									   ; 

							nocache = Math.random();
					//         Pass the login variables like URL variable
							http.open('post', request+'&nocache = '+nocache);
							http.onreadystatechange = MaterialReply;
							http.send(null);

						}else if('delete' == action){
							if(confirm('Удалить?')){
							   var request = '/core/Material/MaterialAction.php' + '?' + 'action=delete' + '&id=' + id; 
								nocache = Math.random();
						//         Pass the login variables like URL variable
								http.open('post', request+'&nocache = '+nocache);
								http.onreadystatechange = MaterialReply;
								http.send(null);
							}
							   
						}else 
							alert('Unknown command');
		//MatGroup


		}
		
		//------------------- Order --------------------------------------------------------
		else if(page == 'Order'){

						if('product' == action){
							var code = encodeURI(document.getElementById('MaterialID').value);
							var request = '/core/Order/OrderAction.php' + '?' + 'action=product'
										   + '&code=' + code; 

							nocache = Math.random();
					//         Pass the login variables like URL variable
							http.open('post', request+'&nocache = '+nocache);
							http.onreadystatechange = product;
							http.send(null);

						}else if('create' == action){
							var userID = encodeURI(document.getElementById('userID').value);
							var userIDTo = encodeURI(document.getElementById('userIDTo').value);
							var Description = encodeURI(document.getElementById('Description').value);
							var request = '/core/Order/OrderAction.php' + '?' + 'action=create'
										   + '&userID=' + userID
										   + '&userIDTo=' + userIDTo
										   + '&Description=' + Description
											   
									   ; 

							nocache = Math.random();
					//         Pass the login variables like URL variable
							http.open('post', request+'&nocache = '+nocache);
							http.onreadystatechange = OrderReply;
							http.send(null);
						}else if('update' == action){
							ID = encodeURI(document.getElementById('orderID').value);
							Description = encodeURI(document.getElementById('Description').value);
							userIDTo = encodeURI(document.getElementById('userIDTo').value);
							var request = '/core/Order/OrderAction.php' + '?' + 'action=update'
										   + '&ID=' + ID
										   + '&userIDTo=' + userIDTo
										   + '&Description=' + Description 
									   ; 

							nocache = Math.random();
					//         Pass the login variables like URL variable
							http.open('post', request+'&nocache = '+nocache);
							http.onreadystatechange = OrderReply;
							http.send(null);
						}else if('delete' == action){
							if(confirm('Удалить?')){
							   var request = '/core/Order/OrderAction.php' + '?' + 'action=delete' + '&ID=' + id
									   ; 
								nocache = Math.random();
						//         Pass the login variables like URL variable
								http.open('post', request+'&nocache = '+nocache);
								http.onreadystatechange = OrderReply;
								http.send(null);
							}
						}else 
							alert('Unknown command');
		
	}
	// ------------------- OrderLine ----------------------------------------------------
		else if(page == 'OrderLine'){
						if('create' == action){
								G_OrderID = id;
							var OrderID = id;
							var MaterialID = encodeURI(document.getElementById('MaterialID').value);
							var Qty = encodeURI(document.getElementById('Qty').value);
							var Price1 = encodeURI(document.getElementById('Price1').value);
							var Ball = encodeURI(document.getElementById('Ball').value);
							var request = '/core/Order/OrderLineAction.php' + '?' + 'action=create'
										   + '&OrderID=' + OrderID
										   + '&MaterialID=' + MaterialID
										   + '&Qty=' + Qty
											+ '&Price1=' + Price1
											+ '&Ball=' + Ball
									   ; 
							
							nocache = Math.random();
					//         Pass the login variables like URL variable
							http.open('post', request+'&nocache = '+nocache);
							http.onreadystatechange = OrderLineReply;
							http.send(null);
						}else if('update' == action){
							var 	OrderLineID = id;
								G_OrderID = encodeURI(document.getElementById('OrderID').value); 
							var MaterialID = encodeURI(document.getElementById('MaterialID').value);
							var Qty = encodeURI(document.getElementById('Qty').value);
							var Price1 = encodeURI(document.getElementById('Price1').value);
							var Ball = encodeURI(document.getElementById('Ball').value);
							var request = '/core/Order/OrderLineAction.php' + '?' + 'action=update'
										   + '&OrderLineID=' + OrderLineID
										   + '&MaterialID=' + MaterialID
										   + '&Qty=' + Qty
											+ '&Price1=' + Price1
											+ '&Ball=' + Ball
									   ; 
							
							nocache = Math.random();
					//         Pass the login variables like URL variable
							http.open('post', request+'&nocache = '+nocache);
							http.onreadystatechange = OrderLineReply;
							http.send(null);
						}else if('delete' == action){
							if(confirm('Удалить?')){
								G_OrderID = encodeURI(document.getElementById('orderID').value); 
						        var request = '/core/Order/OrderLineAction.php' + '?' + 'action=delete' + '&OrderLineID=' + id
									   ; 
								nocache = Math.random();
						//         Pass the login variables like URL variable
								http.open('post', request+'&nocache = '+nocache);
								http.onreadystatechange = OrderLineReply;
								http.send(null);
							}
						}else 
							alert('Unknown command');





	}
	else
        alert('Unknown page');
}

function UserReply() {
    if(http.readyState == 4){
//        $("#context").load('/core/User/UserView.html');

	var fr = window.parent.document.getElementById('context');
	if(fr != null){
		fr.src = '/core/User/UserView.php';

	}
    }
}

function MatGroupReply() {
    if(http.readyState == 4){
//        $("#context").load('/core/MatGroup/MatGroupView.php');

	var fr = window.parent.document.getElementById('context');
	if(fr != null){
		fr.src = '/core/MatGroup/MatGroupView.php';
	}
    }
}

function MaterialReply() {
    if(http.readyState == 4){
//        $("#context").load('/core/Material/MaterialView.php');

	var fr = window.parent.document.getElementById('context');
	if(fr != null){
		fr.src = '/core/Material/MaterialView.php';
	}
    }
}

function DimensionReply() {
    if(http.readyState == 4){
//        $("#context").load('/core/Dimension/DimensionView.php');
   
	var fr = window.parent.document.getElementById('context');
	if(fr != null){
		fr.src = '/core/Dimension/DimensionView.php';

	}
 }
}

function OrderReply() {
    if(http.readyState == 4){
//        $("#context").load('/core/Order/OrderView.php');

	var fr = window.parent.document.getElementById('context');
	if(fr != null){
		fr.src = '/core/Order/OrderView.php';
	}
    }
}

function product() {
	alert(http);	
    if(http.readyState == 4){
//        $("#context").load('/core/Order/OrderView.php');

/*	var fr = window.parent.document.getElementById('context');
	if(fr != null){
		fr.src = '/core/Order/OrderView.php';

	}
*/    

	}
}

function OrderLineReply() {
    if(http.readyState == 4){

	var fr = window.parent.document.getElementById('context');
	if(fr != null){
		fr.src = '/core/Order/OrderUpdate.php?id=' + G_OrderID;
	}
//        $("#context").load('/core/Order/OrderUpdate.php?id=' + G_OrderID);
    }
}


function timedRedirect(time, url) {
    redirectTime = time;
    redirectURL = url;
    setTimeout("location.href = redirectURL;",redirectTime);
}


function silentDeleteOrder(){
	
}

function getMaterialDetail(){
	var MaterialID = document.getElementById('MaterialID').value;
	var request = '/core/Order/getMatDetail.php' + '?' + 'MaterialID=' + MaterialID
			   ; 
		nocache = Math.random();
//         Pass the login variables like URL variable
		http.open('post', request+'&nocache = '+nocache);
		http.onreadystatechange = ans;
		http.send(null);
/*		
	if(http.readyState == 4){
        var response = http.responseText;
        alert(response);
        // else if login is ok show a message: "Site added+ site URL".
			document.getElementById('MatName').value = response;
        //setTimeout('', 2000);
        //$("#context").load('/core/Order/OrderView.html');
        //timedRedirect(1500,  "workflow.php");
        
    }
*/
}
	
function ans(){
	if(http.readyState == 4){   
	    var response = http.responseText;

        var data = jQuery.parseJSON(response);
		//alert(data.MatName + ' : ' + data.Price1);
        // else if login is ok show a message: "Site added+ site URL".
			document.getElementById('MatName').value = data.MatName;
			document.getElementById('Price1').value = data.Price1;
			document.getElementById('Ball').value = data.Ball;
	}
}

function isNumberAndCalc(){
		var Qty = document.getElementByID('Qty').value;
		alert(Qty);
		/*if(Qty!=null){
			if(Qty > 0){
							
			}
		}*/
}

$(".numeric").keypress(function() {
    return (/\d/.test(String.fromCharCode(event.which) ))
});


function getPage(page){
	var fr = window.parent.document.getElementById('context');
	if(fr != null){
		fr.src = page;
	}
	//$("#context").load(page);
}


function calc(){
	document.getElementById('Summ').value = Number(document.getElementById('Price1').value) * Number(document.getElementById('Qty').value);
	document.getElementById('Balls').value = Number(document.getElementById('Ball').value) * Number(document.getElementById('Qty').value);
};




